package org.eclipse.wtp.sample.flashcards;

import java.util.*; // for ArrayList
import java.io.*; 	// for BufferedReader and BufferedWriter
import java.util.Collections;


public class FlashCardSet {
	private String title;	// name of the set of FlashCards. eg. "Spanish-English"
	private String category1;// eg. "Spanish"
	private String category2;// eg. "English"
	private String filename;

	private ArrayList<FlashCard> set; 	// the set containing all flash cards
	private Random random;	// for generating random integers

	/* Constructors */
	public FlashCardSet(String title, String category1, String category2) {
		this.title = title;
		this.category1 = category1;
		this.category2 = category2;
		this.set = new ArrayList<FlashCard>();
		this.filename = title + ".txt";
		this.random = new Random();
	}
	public FlashCardSet(String title) {
		this(title, "", "");
	}

	
	/* Public Interface */
	public String getTitle() 	{return this.title;}
	public String getCategory1()  {return this.category1;}
	public String getCategory2()  {return this.category2;}
	public String getFilename()	{return this.filename;}
	public int size() {return this.set.size();}
	public Iterator<FlashCard> iterator() {return set.iterator();}
	public void sort(int sortByKey) {
		if (sortByKey == 1) {
			Collections.sort(set, new Comparator() {
				public int compare (Object o1, Object o2) {
					FlashCard f1 = (FlashCard) o1;
					FlashCard f2 = (FlashCard) o2;
					return f1.getKey1().compareToIgnoreCase(f2.getKey1());
				}
			});

		}
		else if (sortByKey == 2) {
			Collections.sort(set, new Comparator() {
				public int compare (Object o1, Object o2) {
					FlashCard f1 = (FlashCard) o1;
					FlashCard f2 = (FlashCard) o2;
					return f1.getKey2().compareToIgnoreCase(f2.getKey2());
				}
			});
		}
	}
	public String lookupByKey1(String key) {
		for(int i = 0; i < this.set.size(); i++) {
			if (this.set.get(i).getKey1().equals(key)) {
				return this.set.get(i).getKey2();
			}
		}
		return "";
	}
	public String lookupByKey2(String key) {
		for(int i = 0; i < this.set.size(); i++) {
			if (this.set.get(i).getKey2().equals(key)) {
				return this.set.get(i).getKey1();
			}
		}
		return "";
	}
	public void addCard(String key1, String key2, String image){
		FlashCard newCard = new FlashCard(key1, key2, image);
		this.set.add(newCard);
	}
	public void remove(String word1, String word2) {
		Iterator itr = set.iterator();
		while (itr.hasNext()) {
			FlashCard card = (FlashCard)itr.next();
			if (card.getKey1().equals(word1) && card.getKey2().equals(word2)) {
				itr.remove();
			}
		}
	}
	public FlashCard drawCard() {
		int size = this.set.size();
		int index = random.nextInt(size);
		return this.set.get(index);
	}
	public void save(BufferedWriter out) {
				
		try {
			// write category names
			out.write(this.category1 + "\n");
			out.write(this.category2 + "\n");
	
			Iterator<FlashCard> itr = set.iterator();
			while(itr.hasNext()) {
				FlashCard card = (FlashCard)itr.next();
				out.write(card.getKey1() + "\n");
				out.write(card.getKey2() + "\n");
				out.write(card.getImage() + "\n");
			}
		}
		catch (IOException e) {
			
		}
	}
	public void load(BufferedReader reader) {
		String temp = "";
		try{
		// read category titles
		temp = reader.readLine();
		if (temp != null) {this.category1 = temp;}
		temp = reader.readLine();
		if (temp != null) {this.category2 = temp;}

		while ((temp = reader.readLine()) != null) {
			// read one entry
			if (temp != null) {
				String key1 = temp;
				temp = reader.readLine();
				if (temp != null) {
					String key2 = temp;
					temp = reader.readLine();
									
					this.addCard(key1, key2, ""); // not reading data for image
					
				}
			}
		}
		}
		catch (IOException e) {
			
		}
	}
}